package com.neusoft.training.podcast.mapper;

import java.sql.Date;
import java.sql.Time;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.training.podcast.entity.Video;

public interface VideoMapper {
	
	//获取所有视频
	@Select("select * from video where videoIsDel = 0 ")
	List<Video> findAllVideos();
	
	//添加视频
	@Insert("insert into video(videoID,title,description,totalTime,uploadTime,size,authorID,categoryID,extendName,filePath,videoIsDel,cover) "
			+ "values(#{videoID},#{title},#{description},#{totalTime},#{uploadTime},#{size},#{authorID},#{categoryID},#{extendName},#{filePath}, #{videoIsDel},#{cover})")
	void addVideo(@Param("videoID") String videoID,
			@Param("title") String title,
			@Param("totalTime") Time totalTime,
			@Param("uploadTime") Date uploadTime,
			@Param("size") Integer size,
			@Param("authorID") Integer authorID,
			@Param("categoryID") Integer categoryID,
			@Param("extendName") String extendName,
			@Param("filePath") String filePath,
			@Param("videoIsDel") Integer videoIsDel,
			@Param("cover") String cover
			);
	@Insert("insert into video(videoID,title,description,totalTime,uploadTime,size,authorID,categoryID,extendName,filePath,videoIsDel,cover) "
			+ "values(#{videoID},#{title},#{description},#{totalTime},#{uploadTime},#{size},#{authorID},#{categoryID},#{extendName},#{filePath}, #{videoIsDel},#{cover})")
	void saveVideo(Video video);
	
	//根据视频ID删除视频
	@Delete("delete from video where videoID=#{videoID}")
	void cleanVideo(@Param("videoID") String videoID);
	
	//根据视频类别号计算出该类别个数
	@Select("select count(*) from video where categoryID=#{categoryID}")
	int countCategoryVideo(@Param("categoryID") Integer categoryID);
	
	//根据视频类别号搜索视频列表
	@Select("select * from video where categoryID=#{categoryID}")
	List<Video> findCategoryVideo(@Param("categoryID") Integer categoryID);
	
	//根据视频ID号找到视频
	@Select("select * from video where videoID=#{videoID}")
	Video findVideoByID(@Param("videoID") String videoID);
	
	//根据视频名找到视频
	@Select("select * from video where title=#{title}")
	List<Video> findVideoByName(@Param("title") String title);
		
	
	
	//根据用户ID显示上传的视频
	@Select("select * from video where authorID=#{authorID} order by clickCount desc")
	List<Video> findVideoOfAuthor(@Param("authorID") Integer authorID);
	
	//根据视频ID更新videoIsDel
	@Update("update video set videoIsDel=1 where videoID=#{videoID}")
	void deleteVideo(@Param("videoID") String videoID);
	
	//根据视频ID更新视频点击量
	@Update("update video set clickCount=clickCount + 1 where videoID=#{videoID}")
	void updateVideoClickCount(@Param("videoID") String videoID);
	
	//根据视频ID更新视频点赞数
	@Update("update video set recommentCount=#{recommentCount} where videoID=#{videoID}")
	void updateVideoRecommentCount(@Param("videoID") String videoID, @Param("recommentCount") Integer recommentCount);
	
	//根据视频ID更新videoCover
	@Update("update video set cover=#{cover} where videoID=#{videoID}")
	void changeVideoCover(Video video);
	
	//根据视频名模糊搜索视频
	@Select("select * from video where title like CONCAT(CONCAT('%', #{title}), '%')")
	List<Video> findVideoByMName(@Param("title") String title);
	
	//后端逻辑添加 author：llx
	
//		@Select("select distinct * from video where title=#{title}")
//		Video findVideoByName(String title);
		
		@Select("select distinct * from video where videoID in (select favouriteVedioID from favourite where favouriteUserID=#{favouriteUserID})")
		List<Video> findFavouriteVideo(@Param("favouriteUserID") Integer favouriteUserID);
		
		@Select("select distinct * from video where videoID in (select commentVideoID from comment where commentUserID=#{userID})")
		List<Video> findCommentVideo(@Param("userID") Integer userID);

}
